<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Gate extends CI_Controller {

	function __construct()
	{
		parent::__construct();
		$this->load->library('smarty3', '', 'smarty');
                
                require_once APPPATH.'libraries/pay/fpt/gate-api.php';
	}
	// --------------------------------------------------------------------
	function index()
	{
		$this->smarty->display('template/frontend/404');
	}
        
        function getMultiCard()
        {
            /*
             * OrginalData:
                + CardType: loại thẻ
                + CardAmount: mệnh giá thẻ
                + CardQuantity: số lượng thẻ
             */
            //giả lập dữ liệu
            $CardType       = 'VIETTEL';
            $CardAmount     = 10000;
            $CardQuantity   = 2;
            
            //$result         = _gate_GetMultiCard($CardType, $CardAmount, $CardQuantity);
            
            //print_r($result);
            
            //Inlude lirary nusoap
            
            require_once(APPPATH.'../library/nusoap/nusoap.php');
            require_once(APPPATH.'libraries/pay/fpt/FPT_Libs.php');
            require_once(APPPATH.'libraries/pay/fpt/client.php');
            /*
            $proxyhost = isset($_POST['proxyhost']) ? $_POST['proxyhost'] : '';
            $proxyport = isset($_POST['proxyport']) ? $_POST['proxyport'] : '';
            $proxyusername = isset($_POST['proxyusername']) ? $_POST['proxyusername'] : '';
            $proxypassword = isset($_POST['proxypassword']) ? $_POST['proxypassword'] : '';
            $client = new nusoap_client('https://sandbox-ops.gate.vn:7001/Igate_WS/Route?wsdl', 'wsdl',
            $proxyhost, $proxyport, $proxyusername, $proxypassword);
            */
            
            $client = new nusoap_client('http://test.daily.gate.vn/api/multicard/ws.asmx', 'wsdl');
            $err = $client->getError();
            if ($err) {
                echo '<h2>Constructor error</h2><pre>' . $err . '</pre>';
            }
            // Doc/lit parameters get wrapped

            $ServiceID = "CardOutputMulti";

            $rq = new RequestData();
            $rq->Username = '0989935052';
            $rq->Password = 'duongan';
            $rq->CardName = 'VIETTEL';
            $rq->Quantity = 3;
            $rq->ReturnCode = '';
            $rq->Message = '';
            $rq->Balance = '';
            $rq->TransID = '123456789';
            $rq->Amount = 10000;
//            $rq->MerchantID = 1105;
//            $rq->CardType = "VIETTEL";
//            $rq->CardAmount = 20000;
//            $rq->CardQuantity = 3;
//            $rq->Amount = 10000;
//            $rq->FunctionName = "BuyCard";
//            $rq->PartnerTransactionID = rand(10000000, 110000000);

//            $oXMLWriter = new XMLWriter;
//            $oXMLWriter->openMemory();
//            $oXMLWriter->startDocument('1.0', 'UTF-8');
//
//            $oXMLWriter->startElement('BuyCard');
//            
//            //username
//            $oXMLWriter->startElement('username'); 
//            $oXMLWriter->writeRaw('0989935052');
//            $oXMLWriter->endElement();
//            //password
//            $oXMLWriter->startElement('password'); 
//            $oXMLWriter->writeRaw('duongan');
//            $oXMLWriter->endElement();
//            //cardname
//            $oXMLWriter->startElement('cardName'); 
//            $oXMLWriter->writeRaw('VIETTEL');
//            $oXMLWriter->endElement();
//            //amount
//            $oXMLWriter->startElement('amount'); 
//            $oXMLWriter->writeRaw('10000');
//            $oXMLWriter->endElement();
//            //quatity
//            $oXMLWriter->startElement('quantity'); 
//            $oXMLWriter->writeRaw('3');
//            $oXMLWriter->endElement();
//            //returnCode
//            $oXMLWriter->startElement('returnCode'); 
//            $oXMLWriter->writeRaw('');
//            $oXMLWriter->endElement();
//            //message
//            $oXMLWriter->startElement('massage'); 
//            $oXMLWriter->writeRaw('');
//            $oXMLWriter->endElement();
//            //balance
//            $oXMLWriter->startElement('balance'); 
//            $oXMLWriter->writeRaw('');
//            $oXMLWriter->endElement();
//            //transID
//            $oXMLWriter->startElement('transID'); 
//            $oXMLWriter->writeRaw('123456789');
//            $oXMLWriter->endElement();
//            
//            $oXMLWriter->endElement();
//            
//
//            $oXMLWriter->endDocument();
//            $strxml = $oXMLWriter->outputMemory();
//
//            $oXMLWriter->flush();
//            file_put_contents('test-gate.xml', $strxml);
//            $request = $strxml;
//            
//            $headers = array(
//                "Content-type: text/xml"
//                , "Content-length: " . strlen($request)
//                , "Connection: close"
//            );
//            
//            
//            
//            $ch = curl_init();
//            curl_setopt($ch, CURLOPT_URL, 'http://test.daily.gate.vn/api/multicard/ws.asmx');
//            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//            curl_setopt($ch, CURLOPT_TIMEOUT, 100);
//            curl_setopt($ch, CURLOPT_POST, true);
//            curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
//            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
//
//            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
//            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
//            curl_setopt($ch, CURLOPT_CAINFO, NULL);
//            $data = curl_exec($ch);		
//            
//            print_r($data);
//            print_r($rq);
//            exit;
            
//            $private_key = APPPATH."libraries/pay/fpt/1105.key";
//            $private_key_pass = "02ca0989";
//            $secretkey = "d1e28b44db2d2fe7494d50a5162be3e2";
//
//            $OriginalData = sprintf("%d%s%d%d%s", $rq->MerchantID, $rq->CardType, $rq->CardAmount, $rq->CardQuantity, $secretkey);
//            if (openssl_sign($OriginalData, $Signature, array(file_get_contents($private_key), $private_key_pass))) {
//                $Signature = base64_encode($Signature);
//            }

            //$rq->Signature = $Signature;


            $xmlSerializer = new XMLSerializer();
            $request = $xmlSerializer->generateValidXmlFromObj($rq, "BuyCard");
            file_put_contents('test2-gate.xml', $request);
            $param = array(
                'arg0' => $ServiceID,
                'arg1' => $request
            );

            //print_r($param);

            $result = $client->call('ProcessRequest', $param, '', '', false, true);
            // Check for a fault
            if ($client->fault) {
                echo '<h2>Fault</h2><pre>';
                print_r($result);
                echo '</pre>';
            } else {
                // Check for errors
                $err = $client->getError();
                if ($err) {
                    // Display the error
                    echo '<h2>Error</h2><pre>' . $err . '</pre>';
                } else {
                    // Display the result
                    echo '<h2>Result</h2><pre>';
                    //print_r($result);		

                    $returnString = $result["return"];
                    $splits = spliti("\|", $returnString);
                    if ($splits[0] == "00") {
                        $xmlResults = $splits[5];
                        $xmltable = simplexml_load_string($xmlResults);

                        $key = '@1Pas!sw#or^d';
                        $iv = '%1Az=-@qT';
                        $fpt = new FptEncoder(false, $key, $iv);
                        foreach ($xmltable as $key => $value) {
                            echo "Item: " . $key;
                            $id = $value->ID;
                            $serial = $value->Serial;
                            $password = $value->Password;
                            echo $id . " - " . $serial . " - " . $password;
                            $pin = $fpt->decrypt("" . $password . "");
                            echo " - " . $pin;
                            echo "<br/ >";
                        }
                    } else {
                        echo $returnString;
                    }
                    echo '</pre>';
                }
            }
            echo '<h2>Request</h2><pre>' . htmlspecialchars($client->request, ENT_QUOTES) . '</pre>';
            echo '<h2>Response</h2><pre>' . htmlspecialchars($client->response, ENT_QUOTES) . '</pre>';
            //echo '<h2>Debug</h2><pre>' . htmlspecialchars($client->debug_str, ENT_QUOTES) . '</pre>';
        }
}

/* End of file rss.php */
/* Location: ./system/application/controllers/rss.php */